package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.mo.MessageObject;
import cn.zj.logistics.pojo.BaseData;
import cn.zj.logistics.pojo.BaseDataExample;
import cn.zj.logistics.pojo.BaseDataExample.Criteria;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.service.BaseDataService;

@Controller
@RequestMapping("/baseData")
public class BaseDataController {
	
	
	@Autowired
	private BaseDataService baseDataService;
	
	
	//页面跳转功能
	@RequestMapping("/baseDataPage")
	@RequiresPermissions("baseData:baseDataPage")
	public String baseDataPage() {
		
		return "baseDataPage";
	}
	
	@RequestMapping("/baseDataEdit")
	public String baseDataEdit(Model m,Long baseId) {
		
		//如果用户id不等于空，说明是修改操作，查询用户对象共享过去，以供回显
		if(baseId !=null) {
			BaseData baseData = baseDataService.selectByPrimaryKey(baseId);
			m.addAttribute("baseData", baseData);
		}
		
		
		//查询基础数据所属分类
		
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdIsNull();
		
		List<BaseData> parents = baseDataService.selectByExample(example );
		
		m.addAttribute("parents", parents);
		
		
		return "baseDataEdit";
	}
	
	
	
	
	//新增功能
	@RequestMapping("/insert")
	@RequiresPermissions("baseData:insert")
	@ResponseBody
	public MessageObject insert(BaseData baseData) {
		
		
		MessageObject mo = MessageObject.createMessageObject(0, "新增数据失败，请联系管理员");
		
		int row = baseDataService.insert(baseData);
		if(row == 1) {
			mo = MessageObject.createMessageObject(1, "新增数据成功");
		}
		
		return mo;
	}
	//修改功能
	@RequiresPermissions("baseData:update")
	@RequestMapping("/update")
	@ResponseBody
	public MessageObject update(BaseData baseData) {
		
		MessageObject mo = MessageObject.createMessageObject(0, "修改数据失败，请联系管理员");
		
		int row = baseDataService.updateByPrimaryKeySelective(baseData);
		if(row == 1) {
			mo = MessageObject.createMessageObject(1, "修改数据成功");
		}
		
		return mo;
	}
	
	
	//数据列表功能
	@ResponseBody
	@RequiresPermissions("baseData:list")
	@RequestMapping("/list")
	public PageInfo<BaseData> list(@RequestParam(defaultValue = "1") int pageNum,
				@RequestParam(defaultValue = "10") int pageSize,String keyword){
		
		
		PageHelper.startPage(pageNum, pageSize);
		
		BaseDataExample example = new BaseDataExample();
		
		
		
		//条件搜索查询
		
		if(StringUtils.isNotBlank(keyword)) {
			//基础数据名称查询
			Criteria criteria = example.createCriteria();
			criteria.andBaseNameLike("%"+keyword+"%");
			
			
			Criteria criteria2 = example.createCriteria();
			criteria2.andParentNameLike("%"+keyword+"%");
			
			
			example.or(criteria2);
		}
		List<BaseData> baseDatas = baseDataService.selectByExample(example);

		PageInfo<BaseData> pageInfo = new PageInfo<BaseData>(baseDatas);
		return pageInfo;
	}
	
	@RequiresPermissions("baseData:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long baseId) {
		/*
		 * 删除前，先要根据当前删除的id去查询 有parent_id 是否有当前id的数据
		 * 如果有：不能删除
		 */
		
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(baseId);
		
		List<BaseData> children = baseDataService.selectByExample(example );
		
		
		//创建消息对象
		MessageObject mo = MessageObject.createMessageObject(0, "删除数据失败，请联系管理员");
		
		if(children.size() > 0) {
			mo = MessageObject.createMessageObject(0, "此分类数据还有子数据，不能删除");
		}else {
			int row = baseDataService.deleteByPrimaryKey(baseId);
			
			if(row == 1) {
				mo = MessageObject.createMessageObject(1, "删除数据成功");
			}
			
		}
		
		
		return mo;
	}
	

	
}
